package cn.yunhe.servlet;

import cn.yunhe.service.NewsService;
import cn.yunhe.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
//by郭瀚钰
@WebServlet("/DelNewsServlet")
public class DelNewsServlet  extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Integer> enidList = new ArrayList<Integer>();
        Integer state = null;
        if(request.getParameter("state")!=null) {
            state = Integer.parseInt(request.getParameter("state"));
        }
        if (state==null){
            Integer enid = Integer.parseInt(request.getParameter("enid"));
            enidList.add(enid);
        }
        if (state!=null) {
            String idList = request.getParameter("idStr");
            String[] idstr = idList.split(",");
            for (int i = 0; i < idstr.length; i++) {
                Integer enid = Integer.parseInt(idstr[i]);
                enidList.add(enid);
            }
        }
            NewsService newsService = new NewsService();
            boolean flag = newsService.delNews(enidList);

            PrintWriter out = response.getWriter();
            response.setContentType("text/html;charset=UTF-8");
            out.print(flag);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
